package org.darcy.controller.index;

import java.time.LocalDate;
import java.time.Year;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.darcy.entity.privilege.SysUserDataRange;
import org.darcy.framework.model.R;
import org.darcy.service.privilege.SysUserDataRangeService;
import org.darcy.service.summary.SummaryService;
import org.darcy.utils.UserTool;
import org.darcy.vo.project.CapitalSummary;
import org.darcy.vo.project.HomeSummary;
import org.darcy.vo.project.ProjectSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "数据统计接口")
@RestController
@RequestMapping(value = "/summary")
@Slf4j
public class SummaryController {

	private final String info_prefix = "[数据统计模块]";

	@Autowired
	private SummaryService SummaryService;

	@Autowired
	private SysUserDataRangeService SysUserDataRangeService;

	@Operation(summary = "首页统计数据")
	@PostMapping("/homeSummary")
	public R homeSummary(@RequestBody HomeSummary param) {
		log.debug("{}-首页统计数据", info_prefix);
		if (param.getQueryBegin() == null) {
			LocalDate firstDay = Year.now().atDay(1);
			// 转换为java.util.Date
			Date date = Date.from(firstDay.atStartOfDay() // 转换为当天的起始时间 (00:00)
					.atZone(ZoneId.systemDefault()) // 使用系统默认时区
					.toInstant() // 转换为 Instant
			);
			param.setQueryBegin(date);
		}
		if (param.getQueryEnd() == null) {
			param.setQueryEnd(new Date());
		}
		// 首页统计个人
		param.setUid(UserTool.getLoginUserId());
		HomeSummary data = SummaryService.homeSummary(param);
		return R.ok().data(data);
	}

	@Operation(summary = "项目统计数据")
	@PostMapping("/projectSummary")
	public R projectSummary(@RequestBody ProjectSummary param) {
		log.debug("{}-项目统计数据", info_prefix);
		if (param.getQueryBegin() == null) {
			LocalDate firstDay = Year.now().atDay(1);
			// 转换为java.util.Date
			Date date = Date.from(firstDay.atStartOfDay() // 转换为当天的起始时间 (00:00)
					.atZone(ZoneId.systemDefault()) // 使用系统默认时区
					.toInstant() // 转换为 Instant
			);
			param.setQueryBegin(date);
		}
		if (param.getQueryEnd() == null) {
			param.setQueryEnd(new Date());
		}
		// 项目统计按数据权限来
		Integer uid = UserTool.getLoginUserId();
		List<Integer> deptIdList = SysUserDataRangeService.getDeptIdList(uid);
		if (deptIdList != null) {
			param.setDeptIdList(deptIdList);
		}
		ProjectSummary data = SummaryService.projectSummary(param);
		return R.ok().data(data);
	}

	@Operation(summary = "财务统计数据")
	@PostMapping("/capitalSummary")
	public R capitalSummary(@RequestBody CapitalSummary param) {
		log.debug("{}-财务统计数据", info_prefix);
		if (param.getQueryBegin() == null) {
			LocalDate firstDay = Year.now().atDay(1);
			// 转换为java.util.Date
			Date date = Date.from(firstDay.atStartOfDay() // 转换为当天的起始时间 (00:00)
					.atZone(ZoneId.systemDefault()) // 使用系统默认时区
					.toInstant() // 转换为 Instant
			);
			param.setQueryBegin(date);
		}
		if (param.getQueryEnd() == null) {
			param.setQueryEnd(new Date());
		}
		// 财务统计按数据权限来
		Integer uid = UserTool.getLoginUserId();
		List<Integer> deptIdList = SysUserDataRangeService.getDeptIdList(uid);
		if (deptIdList != null) {
			param.setDeptIdList(deptIdList);
		}
		CapitalSummary data = SummaryService.capitalSummary(param);
		return R.ok().data(data);
	}

}
